const Koa = require('koa')
const bodyParser = require('koa-bodyparser')
const Router = require('koa-router')
const cors = require('koa2-cors')
const { connectDb, initSchema } = require('./database/init')
const app = new Koa()

// 连接数据库，并读取schema下面的文件
connectDb()
initSchema()

app.use(cors())

app.use(bodyParser())

// 加载子路由
const ArticleRouter = require('./database/api/article')
const CommentRouter = require('./database/api/comment')

const router = new Router()
router.use('/mpVue/article', ArticleRouter.routes())
router.use('/mpvue/comment', CommentRouter.routes())
// 加上allowedMethods会自动设置ctx.status的不存在的状态和丰富response.header的响应头内容，会加上Allow
app.use(router.routes()).use(router.allowedMethods())

app.listen(8088, () => {
  console.log('server running is %s', 8088)
})
